package com.itcast.demoone

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.itcast.demoone.http.RetrofitClient
import com.itcast.demoone.model.LoginRequest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class OkHttpActivity : AppCompatActivity() {
    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_ok_http)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 1. 获取渠道列表
        findViewById<Button>(R.id.getChannel).setOnClickListener {
            try {
                lifecycleScope.launch(context = Dispatchers.Main) {
                   // 1.1 切换到IO线程发请求
                   val res = withContext(Dispatchers.IO) {
                       RetrofitClient.apiService.getChannels()
                   }
                   // 1.2 自动切换到主线程显示结果
                   if(res.code == 10000){
                     val data = res.data?.channels
                     findViewById<TextView>(R.id.channel).text = "获取频道列表成功, 频道名称: ${data?.joinToString(", ") { it.name }}"
                   }
                }
            } catch (e: Exception){
                Toast.makeText(this, e.message, Toast.LENGTH_SHORT).show()
            }
        }


        // 2. 登录操作
        findViewById<Button>(R.id.login).setOnClickListener {
            try {
                lifecycleScope.launch {
                    val req = LoginRequest("admin", "123456")
                    val res = withContext(Dispatchers.IO){
                        RetrofitClient.apiService.login(req)
                    }

                    if (res.code == 10000){
                        val token = res.data?.token
                        findViewById<TextView>(R.id.tv).text = "登录成功, Token: $token"
                    } else {
                        // 处理响应
                        Toast.makeText(
                            this@OkHttpActivity,
                            "登录失败",
                            Toast.LENGTH_SHORT
                        ).show()
                    }

                }
            }catch (e: Exception){
                // 处理错误
                Toast.makeText(
                    this@OkHttpActivity,
                    "请求失败",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }


    }
}